/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesis2.pucp.Seguridad.DAO;

import com.tesis2.pucp.Modelo.TesisHorario;
import com.tesis2.pucp.Modelo.TesisPerfil;
import com.tesis2.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pachon
 */
public class PerfilDAO {
    
    public void guardarPerfil(TesisPerfil perfil){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(perfil);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
    
    public ArrayList<TesisPerfil> listarPerfil()
    {
        ArrayList<TesisPerfil> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from TesisPerfil order by idperfil").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                TesisPerfil p = (TesisPerfil) iter.next();
                Hibernate.initialize(p.getDescripcion());
                Hibernate.initialize(p.getEstado());
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }    
    
}
